jQuery.fn.extend({
	uploadPreview : function(opts) {
		var _self = this, _this = $(this);
		opts = jQuery.extend({
			Img : "ImgPr",
			Width : 100,
			Height : 100,
			ImgType : [ "jpeg", "jpg", "png" ],
			Callback : function() {
			}
		}, opts || {});
		_self.getObjectURL = function(file) {
			var url = null;
			if (window.createObjectURL != undefined) {
				url = window.createObjectURL(file);
			} else if (window.URL != undefined) {
				url = window.URL.createObjectURL(file);
			} else if (window.webkitURL != undefined) {
				url = window.webkitURL.createObjectURL(file);
			}
			return url;
		};
		_this.change(function() {
			if (this.value) {
				if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(
						this.value.toLowerCase())) {
					alert("选择文件错误,图片类型必须是" + opts.ImgType.join("，") + "中的一种");
					this.value = "";
					return false;
				}
				if (this.id == "personalPhoto") {
					var val = document.getElementById("personalPhoto").value;
					// if(s==""){alert("No image,please select
					// again!");document.RegForm.Img_1.focus(); return false;}
					var img = new Image();
					img.src = val;
					var size;
					if ($.browser.msie) {
						if (this.files) {
							size = this.files[0].size / 1024;
							if (size < 1.024 * 9) {
								alert("上传的文件不小于9kb");
								this.src = "";
								return false;
							}
						}
					} else {
						file_size = this.files[0].size;
						var size = file_size / 1024;
						if (size < 1.024 * 9) {
							alert("上传的文件不小于9kb");
							this.files[0].src = "";
							return false;
						}
					}
				}
				var src = "";
				src = _self.getObjectURL(this.files[0]);
				$("#" + opts.Img).attr('src', src);
				opts.Callback(src);
			}
		});
	}
});

/*
 * 用自订样式替换file事件以便替换难看的file标签样式： div_Element：用以替换的标签，即点击该控件时执行file的事件
 * file_Element：file控件
 */
function addEventForFile(div_Element, file_Element) {
	$(div_Element)
			.mouseover(
					function() {
						var move = $(file_Element)[0];
						var scrollTop = document.body.scrollTop
								|| document.documentElement.scrollTop;
						var scrollLeft = document.body.scrollLeft
								|| document.documentElement.scrollLeft;
						var disY = this.offsetTop - scrollTop
								+ move.offsetHeight / 2;
						var disX = this.offsetLeft - scrollLeft
								+ move.offsetWidth / 2;
						this.onmousemove = function(ev) {
							var oEvent = document.all ? event : ev;
							if (oEvent.clientX - disX >= this.offsetWidth
									- move.offsetWidth) {
								move.style.left = (this.offsetWidth - move.offsetWidth)
										+ "px";
							} else if (oEvent.clientX - disX <= 0) {
								move.style.left = 0 + "px";
							} else {
								move.style.left = (oEvent.clientX - disX)
										+ "px";
							}
							if (oEvent.clientY - disY >= this.offsetHeight
									- move.offsetHeight) {
								move.style.top = (this.offsetHeight - move.offsetHeight)
										+ "px";
							} else if (oEvent.clientY - disY <= 0) {
								move.style.top = 0 + "px";
							} else {
								move.style.top = (oEvent.clientY - disY) + "px";
							}
						};
					});
	$(div_Element).mouseout(function() {
		this.onmousemove = null;
	});
	$(file_Element).css({
		'width' : '60px',
		'height' : '20px',
		'position' : 'absolute',
		'opacity' : '0',
		'filter' : 'alpha(opacity=0)',
		'z-index' : 2
	});
}